{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Series的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s1 = Series(np.random.randn(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.306149\n",
       "1   -1.248480\n",
       "2   -0.012338\n",
       "3   -0.255259\n",
       "4    0.479091\n",
       "5    0.331464\n",
       "6    1.739686\n",
       "7    0.535780\n",
       "8   -0.416960\n",
       "9    0.384361\n",
       "dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.30614922, -1.24848047, -0.01233807, -0.25525924,  0.47909092,\n",
       "        0.33146359,  1.73968643,  0.53577959, -0.41696   ,  0.38436085])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=10, step=1)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s2 = s1.sort_values(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6    1.739686\n",
       "0    1.306149\n",
       "7    0.535780\n",
       "4    0.479091\n",
       "9    0.384361\n",
       "5    0.331464\n",
       "2   -0.012338\n",
       "3   -0.255259\n",
       "8   -0.416960\n",
       "1   -1.248480\n",
       "dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.306149\n",
       "1   -1.248480\n",
       "2   -0.012338\n",
       "3   -0.255259\n",
       "4    0.479091\n",
       "5    0.331464\n",
       "6    1.739686\n",
       "7    0.535780\n",
       "8   -0.416960\n",
       "9    0.384361\n",
       "dtype: float64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.sort_index()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Dataframe的排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df1 = DataFrame(np.random.randn(40).reshape(8,5), columns=['A','B','C','D','E'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.111131</td>\n",
       "      <td>1.827146</td>\n",
       "      <td>-1.034404</td>\n",
       "      <td>0.306836</td>\n",
       "      <td>-0.933289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.511803</td>\n",
       "      <td>0.347962</td>\n",
       "      <td>0.183116</td>\n",
       "      <td>-0.342507</td>\n",
       "      <td>-1.062975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.706005</td>\n",
       "      <td>-0.067221</td>\n",
       "      <td>-1.376772</td>\n",
       "      <td>-0.661213</td>\n",
       "      <td>0.874486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.386375</td>\n",
       "      <td>-0.086519</td>\n",
       "      <td>-0.184012</td>\n",
       "      <td>-0.534909</td>\n",
       "      <td>-0.072016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.225058</td>\n",
       "      <td>0.098309</td>\n",
       "      <td>-0.224717</td>\n",
       "      <td>1.184242</td>\n",
       "      <td>2.056770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1.733564</td>\n",
       "      <td>0.090001</td>\n",
       "      <td>-0.737229</td>\n",
       "      <td>-1.561633</td>\n",
       "      <td>-0.855184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.589512</td>\n",
       "      <td>0.903856</td>\n",
       "      <td>-1.053208</td>\n",
       "      <td>1.381474</td>\n",
       "      <td>2.015115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.144392</td>\n",
       "      <td>-1.106281</td>\n",
       "      <td>1.881502</td>\n",
       "      <td>1.553821</td>\n",
       "      <td>1.503359</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -0.111131  1.827146 -1.034404  0.306836 -0.933289\n",
       "1  0.511803  0.347962  0.183116 -0.342507 -1.062975\n",
       "2 -1.706005 -0.067221 -1.376772 -0.661213  0.874486\n",
       "3  0.386375 -0.086519 -0.184012 -0.534909 -0.072016\n",
       "4 -0.225058  0.098309 -0.224717  1.184242  2.056770\n",
       "5 -1.733564  0.090001 -0.737229 -1.561633 -0.855184\n",
       "6 -0.589512  0.903856 -1.053208  1.381474  2.015115\n",
       "7 -0.144392 -1.106281  1.881502  1.553821  1.503359"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5   -1.733564\n",
       "2   -1.706005\n",
       "6   -0.589512\n",
       "4   -0.225058\n",
       "7   -0.144392\n",
       "0   -0.111131\n",
       "3    0.386375\n",
       "1    0.511803\n",
       "Name: A, dtype: float64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1['A'].sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df2 = df1.sort_values('A')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1.733564</td>\n",
       "      <td>0.090001</td>\n",
       "      <td>-0.737229</td>\n",
       "      <td>-1.561633</td>\n",
       "      <td>-0.855184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.706005</td>\n",
       "      <td>-0.067221</td>\n",
       "      <td>-1.376772</td>\n",
       "      <td>-0.661213</td>\n",
       "      <td>0.874486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.589512</td>\n",
       "      <td>0.903856</td>\n",
       "      <td>-1.053208</td>\n",
       "      <td>1.381474</td>\n",
       "      <td>2.015115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.225058</td>\n",
       "      <td>0.098309</td>\n",
       "      <td>-0.224717</td>\n",
       "      <td>1.184242</td>\n",
       "      <td>2.056770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.144392</td>\n",
       "      <td>-1.106281</td>\n",
       "      <td>1.881502</td>\n",
       "      <td>1.553821</td>\n",
       "      <td>1.503359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.111131</td>\n",
       "      <td>1.827146</td>\n",
       "      <td>-1.034404</td>\n",
       "      <td>0.306836</td>\n",
       "      <td>-0.933289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.386375</td>\n",
       "      <td>-0.086519</td>\n",
       "      <td>-0.184012</td>\n",
       "      <td>-0.534909</td>\n",
       "      <td>-0.072016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.511803</td>\n",
       "      <td>0.347962</td>\n",
       "      <td>0.183116</td>\n",
       "      <td>-0.342507</td>\n",
       "      <td>-1.062975</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "5 -1.733564  0.090001 -0.737229 -1.561633 -0.855184\n",
       "2 -1.706005 -0.067221 -1.376772 -0.661213  0.874486\n",
       "6 -0.589512  0.903856 -1.053208  1.381474  2.015115\n",
       "4 -0.225058  0.098309 -0.224717  1.184242  2.056770\n",
       "7 -0.144392 -1.106281  1.881502  1.553821  1.503359\n",
       "0 -0.111131  1.827146 -1.034404  0.306836 -0.933289\n",
       "3  0.386375 -0.086519 -0.184012 -0.534909 -0.072016\n",
       "1  0.511803  0.347962  0.183116 -0.342507 -1.062975"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.111131</td>\n",
       "      <td>1.827146</td>\n",
       "      <td>-1.034404</td>\n",
       "      <td>0.306836</td>\n",
       "      <td>-0.933289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.511803</td>\n",
       "      <td>0.347962</td>\n",
       "      <td>0.183116</td>\n",
       "      <td>-0.342507</td>\n",
       "      <td>-1.062975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.706005</td>\n",
       "      <td>-0.067221</td>\n",
       "      <td>-1.376772</td>\n",
       "      <td>-0.661213</td>\n",
       "      <td>0.874486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.386375</td>\n",
       "      <td>-0.086519</td>\n",
       "      <td>-0.184012</td>\n",
       "      <td>-0.534909</td>\n",
       "      <td>-0.072016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.225058</td>\n",
       "      <td>0.098309</td>\n",
       "      <td>-0.224717</td>\n",
       "      <td>1.184242</td>\n",
       "      <td>2.056770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1.733564</td>\n",
       "      <td>0.090001</td>\n",
       "      <td>-0.737229</td>\n",
       "      <td>-1.561633</td>\n",
       "      <td>-0.855184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.589512</td>\n",
       "      <td>0.903856</td>\n",
       "      <td>-1.053208</td>\n",
       "      <td>1.381474</td>\n",
       "      <td>2.015115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.144392</td>\n",
       "      <td>-1.106281</td>\n",
       "      <td>1.881502</td>\n",
       "      <td>1.553821</td>\n",
       "      <td>1.503359</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -0.111131  1.827146 -1.034404  0.306836 -0.933289\n",
       "1  0.511803  0.347962  0.183116 -0.342507 -1.062975\n",
       "2 -1.706005 -0.067221 -1.376772 -0.661213  0.874486\n",
       "3  0.386375 -0.086519 -0.184012 -0.534909 -0.072016\n",
       "4 -0.225058  0.098309 -0.224717  1.184242  2.056770\n",
       "5 -1.733564  0.090001 -0.737229 -1.561633 -0.855184\n",
       "6 -0.589512  0.903856 -1.053208  1.381474  2.015115\n",
       "7 -0.144392 -1.106281  1.881502  1.553821  1.503359"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2.sort_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-0.111131</td>\n",
       "      <td>1.827146</td>\n",
       "      <td>-1.034404</td>\n",
       "      <td>0.306836</td>\n",
       "      <td>-0.933289</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.511803</td>\n",
       "      <td>0.347962</td>\n",
       "      <td>0.183116</td>\n",
       "      <td>-0.342507</td>\n",
       "      <td>-1.062975</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1.706005</td>\n",
       "      <td>-0.067221</td>\n",
       "      <td>-1.376772</td>\n",
       "      <td>-0.661213</td>\n",
       "      <td>0.874486</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.386375</td>\n",
       "      <td>-0.086519</td>\n",
       "      <td>-0.184012</td>\n",
       "      <td>-0.534909</td>\n",
       "      <td>-0.072016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.225058</td>\n",
       "      <td>0.098309</td>\n",
       "      <td>-0.224717</td>\n",
       "      <td>1.184242</td>\n",
       "      <td>2.056770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1.733564</td>\n",
       "      <td>0.090001</td>\n",
       "      <td>-0.737229</td>\n",
       "      <td>-1.561633</td>\n",
       "      <td>-0.855184</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>-0.589512</td>\n",
       "      <td>0.903856</td>\n",
       "      <td>-1.053208</td>\n",
       "      <td>1.381474</td>\n",
       "      <td>2.015115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>-0.144392</td>\n",
       "      <td>-1.106281</td>\n",
       "      <td>1.881502</td>\n",
       "      <td>1.553821</td>\n",
       "      <td>1.503359</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          A         B         C         D         E\n",
       "0 -0.111131  1.827146 -1.034404  0.306836 -0.933289\n",
       "1  0.511803  0.347962  0.183116 -0.342507 -1.062975\n",
       "2 -1.706005 -0.067221 -1.376772 -0.661213  0.874486\n",
       "3  0.386375 -0.086519 -0.184012 -0.534909 -0.072016\n",
       "4 -0.225058  0.098309 -0.224717  1.184242  2.056770\n",
       "5 -1.733564  0.090001 -0.737229 -1.561633 -0.855184\n",
       "6 -0.589512  0.903856 -1.053208  1.381474  2.015115\n",
       "7 -0.144392 -1.106281  1.881502  1.553821  1.503359"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
